package com.awesome.ipcdemo.messenger;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.awesome.sdk.util.ShowLogUtil;

/**
 * Author: zhangjingfang
 * Email:zhangjingfang@jeejio.com
 * Date: 2021/8/8 8:09 上午
 * Description:服务端
 * 在onBind方法中创建Messenger，关联接收消息的Handler调用getBinder获取Binder对象，在handlerMessage方法中接收客户端发来的消息。
 */
public class MessengerService extends Service {
    public static final int MSG_FROM_CLIENT = 100;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case MSG_FROM_CLIENT:
                    ShowLogUtil.info("收到客户端信息=" + msg.getData().get("msg"));
                    break;
            }
        }
    };

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        ShowLogUtil.info("onBind");
        return new Messenger(mHandler).getBinder();
    }
}